package Algorithm.PrefixSum;
// https://leetcode-cn.com/problems/rotate-function/
public class Leetcode396_旋转数组 {
    class Solution {
        public int maxRotateFunction(int[] nums) {
            int n = nums.length;
            int[] alpha = new int[n];
            int max = Integer.MIN_VALUE;

            int sum = 0;
            int sum_1 = 0;

            for(int i = 0; i < n; i++){
                alpha[i] = i;
                sum += alpha[i] * nums[i];
                sum_1 += nums[i];
            }
            max = Math.max(max, sum);

            int index = n-1;
            for(int i = 0; i < n; i++){
                sum = sum + sum_1 - n * nums[index];
                index--;
                max = Math.max(max, sum);
            }

            return max;
        }
    }
}
